﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HYI.SDBSERVER;
using Sunny.UI;

namespace SDBSERVER
{
    public partial class FMSetGridView : Form
    {
        public string _UserId = string.Empty; //考虑不同用户使用机器分辨率不同，允许用户自行设定界面
        private string _MenuId = string.Empty;
        public DataGridViewTextBoxEditingControl CellEdit = null;

        public FMSetGridView()
        {
            InitializeComponent();
        }

        //载入表格
        public void doLoadGrid(String sMenuId, DataGridView myGridView)
        {
            //收集表格信息
            _MenuId = sMenuId;
            DataTable dt = new DataTable();
            dt.Columns.Add("fEntryId", Type.GetType("System.Int32"));
            dt.Columns.Add("fColName", Type.GetType("System.String"));
            dt.Columns.Add("fColCaption", Type.GetType("System.String"));
            dt.Columns.Add("fColLength", Type.GetType("System.Int32"));
            dt.Columns.Add("fControl", Type.GetType("System.Int32")); //列的 readonly
            dt.Columns.Add("fDispFmt", Type.GetType("System.Int32")); //列的 显示格式 主要是日期  0 无,1 yyyy-MM-dd,2 yyyy-MM-dd hh:mm:ss

            int iROW = 0;

            //遍历GridView 
            foreach (DataGridViewColumn col in myGridView.Columns)
            {
                string sName = col.Name;
                if (col.HeaderText == "")
                {
                    continue;
                }
                iROW = iROW + 1;
                DataRow dr2 = dt.NewRow();
                dr2["fEntryId"] = iROW.ToString();
                dr2["fColName"] = col.Name;
                dr2["fColCaption"] = col.HeaderText;
                dr2["fColLength"] = col.Width.ToString();
                if (col.Visible == false)
                {
                    dr2["fColLength"] = "0";
                }
                dr2["fControl"] = 1;
                if (col.ReadOnly)
                {
                    dr2["fControl"] = 0;
                }
                if (col.Tag != null)
                {
                    dr2["fDispFmt"] = col.Tag.ToString();
                }
                else { dr2["fDispFmt"] = "0"; }
                dt.Rows.Add(dr2);
            }
            GridView1.DataSource = dt;
            GridView1.AllowUserToAddRows = false;//關閉dataGridview中最後一個空白行。

            GridView1.Columns[0].HeaderText = "序号";
            GridView1.Columns[0].ReadOnly = true;
            GridView1.Columns[0].Width = 85;

            GridView1.Columns[1].HeaderText = "字段名";
            GridView1.Columns[1].ReadOnly = true;
            GridView1.Columns[1].Width = 115;

            GridView1.Columns[2].HeaderText = "显示名称";
            GridView1.Columns[2].ReadOnly = false;
            GridView1.Columns[2].Width = 125;

            GridView1.Columns[3].HeaderText = "长度";
            GridView1.Columns[3].Width = 85;

            GridView1.Columns[4].HeaderText = "可编辑";
            GridView1.Columns[4].Width = 85;

            GridView1.Columns[5].HeaderText = "日期格式";
            GridView1.Columns[5].Width = 85;

            //进制排序
            for (int i = 0; i < this.GridView1.Columns.Count; i++)
            {
                this.GridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                //this.GridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            }
        }

        //添加EditingControlShowing事件
        private void Cells_KeyPress(object sender, KeyPressEventArgs e) //自定义事件
        {
            if (this.GridView1.CurrentCellAddress.X == 3)//获取当前处于活动状态的单元格索引
            {
                if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) e.Handled = true;
                if (e.KeyChar == '\b') e.Handled = false;
            }
        }

        //限制某列只能输入数字
        private void GridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.GridView1.CurrentCellAddress.X == 3)//获取当前处于活动状态的单元格索引
            {
                CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                CellEdit.SelectAll();
                CellEdit.KeyPress += Cells_KeyPress; //绑定事件
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            if (this.ShowAskDialog("确定保存吗?"))
            {
                DBSERVER myADO = new DBSERVER();
                myADO.SQLList.Clear();

                string SQL = string.Empty;
                SQL = "delete from TS_COLUMNS "
                 + " where fTableName='" + _MenuId + "'";
                 //+ " and fTableDisplay='"+_UserId+"'";
                myADO.SQLList.Add(SQL);
                int row = GridView1.Rows.Count;//得到总行数    
                for (int i = 0; i < row; i++)//得到总行数并在之内循环    
                {
                    string sfColName = GridView1.Rows[i].Cells[1].Value.ToString();
                    string sfColCaption = GridView1.Rows[i].Cells[2].Value.ToString();
                    string sfColLength = GridView1.Rows[i].Cells[3].Value.ToString();
                    string sfControl = GridView1.Rows[i].Cells[4].Value.ToString();
                    string sfDispFmt = GridView1.Rows[i].Cells[5].Value.ToString();
                    SQL = "insert into TS_COLUMNS(fTableName,fTableDisplay,fColEng,fColName,fColCaption"
                     +",fColType,fColLength,fColDecimal,fControl,fDispFmt"
                     + ",fCtrl3d,fAlign,fIsRequire,fStatus,fMemo) "
                     + " values('" + _MenuId + "','" + _UserId + "'"
                     + ",'" + sfColName + "','" + sfColName + "'"
                     + ",'" + sfColCaption + "',1," + sfColLength 
                     + ",0,"+ sfControl +","+ sfDispFmt + ",1,1,0,0,'')";
                    //Console.WriteLine(SQL);
                    myADO.SQLList.Add(SQL);
                }

                SQL = "update TABLEVERSION set VERSION=VERSION+1 where TABLENAME='TS_COLUMNS'";
                myADO.SQLList.Add(SQL);

                bool IsOk;
                string sError = string.Empty;
                myADO.doSave(out IsOk, out sError);
                if (IsOk)
                {
                    DBCache myCache = new DBCache();
                    myCache.doCache();

                    UIMessageTip.ShowOk("保存成功");
                    this.Close();
                }
            }
        }
    }
}
